<template>
  <view>
    <view class="top">
      <text
        v-for="(item,index) in navTab"
        :key="index"
        @tap="currentTab(index)"
        :class="active==index ? 'top-active' : ''"
      >{{item}}</text>
    </view>
    <!--临停-->
    <view v-if="active==0" class="panel-box">
      <view v-if="totalLT>0">
        <view class="item" v-for="(item,index) in LTorderList" :key="index">
          <view class="item-p1">临停缴费</view>
          <view class="item-p2">{{item.payTime}}</view>
          <view class="item-p3">{{item.park.parkName}}</view>
          <view class="item-p4">
            <text>{{(item.payAmount)/100}}</text>
            <text>元</text>
          </view>
          <view class="item-btn-box">
            <view class="item-p5" @tap="goDetail(1,item.id)">查看详情</view>
            <button v-if="item.invoiceStatus == 0 && item.status == '1' && item.park.isInvoice" class="mini-btn" type="primary" size="mini" @tap="goInvoice(1,item.id,item.payAmount,item.parkId)">开具发票</button>
            <button v-if="item.invoiceStatus > 0 && item.status == '1' && item.park.isInvoice" class="mini-btn" type="primary" size="mini" @tap="goInvoiceLook(1,item)">查看发票</button>
          </view>
          
        </view>
        <view v-if="loading" class="moreText">
          <text>玩命的加载中...</text>
        </view>
        <view v-if="!loading&&!hasMore" class="moreText">
          <text>没有更多内容了</text>
        </view>
      </view>
      <view v-if="totalLT==0&&!loading">
         <view style="text-align:center;padding-top:15%">
          <image src="../../image/empty5.png" style="width: 332rpx;height: 316rpx;"/>
          <view style="padding-top:15px;color:#9B9B9B">当前数据为空</view>
        </view>
      </view>
    </view>
    <!--充值-->
    <view v-if="active==1" class="panel-box">
      <view v-if="totalYQ>0">
        <view class="item" v-for="(item,index) in YQorderList" :key="'YQ'+index">
          <view class="item-p1" v-if="item.carType=='2'">储值卡充值</view>
          <view class="item-p1" v-if="item.carType=='1'">月租卡续费</view>
          <view class="item-p2">{{item.payTime}}</view>
          <view class="item-p3">{{item.park&&item.park.parkName}}</view>
          <view class="item-p4">
            <text>{{(item.payAmount+item.discountAmount)/100}}</text>
            <text>元</text>
          </view>
          <view class="item-btn-box">
             <view class="item-p5" @tap="goDetail(2,item.id)">查看详情</view>
            <button v-if="item.invoiceStatus == 0 && item.status == '1' && item.park.isInvoice" class="mini-btn" type="primary" size="mini" @tap="goInvoice(2,item.id,item.payAmount,item.parkId)">开具发票</button>
            <button v-if="item.invoiceStatus > 0 &&  item.status == '1' && item.park.isInvoice" class="mini-btn" type="primary" size="mini" @tap="goInvoiceLook(2,item)">查看发票</button>
             
          </view>
        </view>
        <view v-if="loading" class="moreText">
          <text>玩命的加载中...</text>
        </view>
        <view v-if="!loading&&!hasMore" class="moreText">
          <text>没有更多内容了</text>
        </view>
      </view>
      <view v-if="YQorderList.length==0&&!loading">
         <view style="text-align:center;padding-top:15%">
          <image src="../../image/empty5.png" style="width: 332rpx;height: 316rpx;"/>
          <view style="padding-top:15px;color:#9B9B9B">当前数据为空</view>
        </view>
      </view>
    </view>
  </view>
</template>
<script>
import Taro from "@tarojs/taro";
import { setGlobalData, getGlobalData } from "../../globalData.js";
import "./orderList.less";
import Empty from "../../components/empty/empty";
import bulterApi from "@souche2014/bulter-api";
const api = bulterApi({ partner: "IOParking" });
export default {
  data() {
    return {
      navTab: ["临停", "充值"],
      active: 0,
      LTorderList: [],
      totalLT: 0,
      YQorderList: [],
      totalYQ: 0,
      hasMore: true,
      isFirstLoad: true,
      loading: true,
      apiBaseUrl: process.env.IOParking.API_HOST, //请求头
      pageIndex: 1,
      pageSize: 10
    };
  },
  components: { Empty },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //this.wx_login();
  },
  onShow: function(){
      this.wx_login();
      this.pageIndex = 1;
      this.isFirstLoad = true;
      this.hasMore = true;
      this.initDataYQ();
  },
  onPullDownRefresh() {
    this.pageIndex = 1;
    this.isFirstLoad = true;
    this.hasMore = true;
    if (this.active == "0") {
      console.log("临停刷新");
      this.initDataLT();
    } else {
      this.initDataYQ();
    }
    Taro.stopPullDownRefresh(); //结束刷新
  },
  onReachBottom() {
    //触发了上拉加载，做些什么
    this.pageIndex = this.pageIndex + 1;
    this.isFirstLoad = false;
    console.log("当前currentTab：",this.active)
    if (this.active == "0") {
      console.log("临停上拉")
      this.initDataLT();
    } else {
      console.log("充值上拉")
      this.initDataYQ();
    }
  },
  methods: {
    wx_login() {
      api
        .getUserInfo({
          scope: "all"
        })
        .then(resp => {
          if (resp.success) {
            console.log("authCode is：", resp.data);
            if (resp.data == "mock") {
              setGlobalData("openId", "oRPxj5TQ040-YRuv0v7lhRvMyRB0");
              setGlobalData("mobile", "13021250990");
              this.initData();
              return;
            }
            this.getInfos(resp.data);
          } else {
            console.log("err is ", resp.message, "err code is ", resp.code);
          }
        });
    },
    getInfos(authCode) {
      let that = this;
      Taro.request({
        url: this.apiBaseUrl + "/weixin/souchegetuser?authCode=" + authCode,
        data: {},
        header: {
          "content-type": "application/json" // 默认值
        },
        method: "GET",
        success(res) {
          console.log(res.data);
          if (res.data.isSuccess) {
            var userOpenId = res.data.data.openId;
            setGlobalData("openId", userOpenId);
            setGlobalData("mobile", res.data.data.phoneNum);
            that.initDataLT();
          }
        }
      });
    },
    initData() {
      this.initDataLT();
    },
    initDataLT: function() {
      
      Taro.showLoading({
        title: "加载中",
        mask: true
      });
      var that = this;
      var base = this.apiBaseUrl;
      that.loading = true;
      Taro.request({
        url: base + "/parkingpayment/pagetempparkingrecord",
        data: {
          pageIndex: that.pageIndex,
          pageSize: that.pageSize,
          filter: {
            mobile: getGlobalData("mobile"),
            wxOpenId: getGlobalData("openId")
          }
        },
        header: {
          "content-type": "application/json" // 默认值
        },
        method: "POST",
        success(res) {
          Taro.hideLoading();
          that.loading = false;
          if (res.data.isSuccess) {
            if (that.isFirstLoad) {
              //初始化
              that.LTorderList = res.data.data.list;
            } else {
              console.log(res.data.data)
              //加载更多追加
              that.LTorderList = that.LTorderList.concat(res.data.data.list);
            }
            if (res.data.data.list.length < that.pageSize) {
              that.hasMore = false;
            }
            that.totalLT = res.data.data.total;
          }
        }
      });
    },
    initDataYQ: function() {
      Taro.showLoading({
        title: "加载中",
        mask: true
      });
      var that = this;
      var base = this.apiBaseUrl;
      that.loading = true;
      Taro.request({
        url: base + "/parkingpayment/pagerenewrecord",
        data: {
          pageIndex: that.pageIndex,
          pageSize: that.pageSize,
          filter: {
            mobile: getGlobalData("mobile"),
            wxOpenId: getGlobalData("openId")
          }
        },
        header: {
          "content-type": "application/json" // 默认值
        },
        method: "POST",
        success(res) {
          Taro.hideLoading();
          that.loading = false;
          if (res.data.isSuccess) {
            if (that.isFirstLoad) {
              //初始化
              that.YQorderList = res.data.data.list;
            } else {
              //加载更多追加
              that.YQorderList = that.YQorderList.concat(res.data.data.list);
            }
            if (res.data.data.list.length < that.pageSize) {
              that.hasMore = false;
            }
            that.totalYQ = res.data.data.total;
          }
        }
      });
    },
    goDetail(type, id) {
      Taro.navigateTo({
        url: "../cardManage/orderListDetail?type=" + type + "&id=" + id
      });
    },
    goInvoice(type, id,jg,parkid) {
      Taro.navigateTo({
        url: "../cardManage/invoice?type=" + type + "&id=" + id +"&jg=" + jg +'&parkid='+parkid
      });
    },
    goInvoiceLook(type,data){
      console.log(data);
      //  Taro.navigateTo({
      //   url: "../cardManage/invoicelook?type=" + type + "&id=" + data.id 
      // });
      let that = this;  
      let base = this.apiBaseUrl;
      Taro.showLoading({
        title: "加载中",
        mask: true
      });
      Taro.request({
        url: base + "/wxinvoice/ordervoice",
        data: {
          keyword: "",
          orderId: data.id,
          orderType: type,
          wxOpenId: getGlobalData("openId")
        },
        header: {
          "content-type": "application/json" // 默认值
        },
        method: "POST",
        success(res) {
          Taro.hideLoading();
          if (res.data.isSuccess) {
              console.log(res);
              //that.fpUrl = res.data.fpUrl
              let useurl = res.data.data.fpUrl.replace('http:', 'https:')
              wx.downloadFile({
              // 示例 url，并非真实存在
              url: useurl,
              success: function (resd) {
                const filePath = resd.tempFilePath
                wx.openDocument({
                  filePath: filePath,
                  showMenu: true,
                  success: function (rest) {
                    console.log('打开文档成功')
                  }
                })
              }
            });
          }
            
        }
      });
      
    },
    currentTab: function(e) {
      if (this.active == e) {
        return;
      }
      this.active = e;
      this.hasMore = true;
      this.loading = true;
      this.isFirstLoad = true;
      if (this.active == "0") {
        this.pageIndex = 1;
        this.initDataLT();
      } else {
        this.pageIndex = 1;
        this.initDataYQ();
      }
    }
  }
};
</script>